package com.common.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.common.R;
import com.common.entity.NoteBean;
import com.common.ui.db.SQLiteDB;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WriteNoteActivity extends AppCompatActivity implements View.OnFocusChangeListener {
    public static final int EXTRA_ADD = 1;
    public static final int EXTRA_UPDATE = 2;
    public static final String EXTRA_TYPE = "EXTRA_TYPE";//是新增还是修改
    public static final String EXTRA_TITLE = "EXTRA_TITLE";//标题
    public static final String EXTRA_CONTENT = "EXTRA_CONTENT";//内容
    public static final String EXTRA_ID = "EXTRA_ID";//备忘录id
    @BindView(R.id.edit_title)
    EditText mEditTitle;
    @BindView(R.id.edit_content)
    EditText mEditContent;
    @BindView(R.id.iv_ok)
    ImageView ivOk;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private int mType = 0;
    private boolean mIsShowOk = false;//是否正在显示完成的按钮--说明这个时候在编辑
    private SQLiteDB mSQLiteDB;
    private String mTitle, mContent, mId;
    private String mNoteCode;
    private List<String> mList = new ArrayList<>();
    private List<String> mStringList = new ArrayList<>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write_note);
        ButterKnife.bind(this);
        mTitle = getIntent().getStringExtra(EXTRA_TITLE);
        mContent = getIntent().getStringExtra(EXTRA_CONTENT);
        mId = getIntent().getStringExtra(EXTRA_ID);
        mSQLiteDB = SQLiteDB.getInstance(this);
        mType = getIntent().getIntExtra(EXTRA_TYPE, mType);
        mEditContent.setOnFocusChangeListener(this);
        mEditTitle.setOnFocusChangeListener(this);
        if (mType == EXTRA_ADD) {
            tvTitle.setText("新增");
            ivOk.setVisibility(View.VISIBLE);
            mIsShowOk = true;
        } else if (mType == EXTRA_UPDATE) {
            tvTitle.setText("编辑");
            mEditTitle.setText(mTitle);
            mEditContent.setText(mContent);
            ivOk.setVisibility(View.GONE);
            mIsShowOk = false;
        }
        ivOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addNote();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.edit_title:
            case R.id.edit_content:
                if (!mIsShowOk && hasFocus) {
                    ivOk.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateNote();
                        }
                    });
                    ivOk.setVisibility(View.VISIBLE);
                    mIsShowOk = true;
                }
        }
    }

    /**
     * 新增
     */
    private void addNote() {
        String title = mEditTitle.getText().toString();
        String content = mEditContent.getText().toString();
        Date date = new Date();
        String addTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
        String updateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
        mNoteCode = date.getTime() + "";
        NoteBean noteBean = new NoteBean();
        noteBean.setTitle(title);
        noteBean.setContent(content);
        noteBean.setAdd_time(addTime);
        noteBean.setUpdate_time(updateTime);
        noteBean.setNote_code(mNoteCode);
        if (mSQLiteDB.addNote(noteBean)) {
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            mIsShowOk = false;
            ivOk.setVisibility(View.GONE);
            mEditContent.clearFocus();
            mEditTitle.clearFocus();
        } else {
            Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 修改
     */
    private void updateNote() {
        NoteBean dataByCode = null;
        if (mType == EXTRA_ADD) {
            dataByCode = mSQLiteDB.findDataByCode(mNoteCode);
        }
        String title = mEditTitle.getText().toString();
        String content = mEditContent.getText().toString();
        Date date = new Date();
        String updateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
        mNoteCode = date.getTime() + "";
        NoteBean noteBean = new NoteBean();
        noteBean.setNote_id(mType == EXTRA_ADD ? dataByCode != null ? dataByCode.getNote_id() : null : mId);
        noteBean.setTitle(title);
        noteBean.setContent(content);
        noteBean.setUpdate_time(updateTime);
        if (mSQLiteDB.updateNote(noteBean)) {
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            mIsShowOk = false;
            ivOk.setVisibility(View.GONE);
            mEditContent.clearFocus();
            mEditTitle.clearFocus();
        } else {
            Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
        }
    }
}
